<?php
include "include/Smarty/Smarty.class.php";

//some defines
define("JS_DIR", "js/");
define("DEBUG", true);


//get the request uri
$strRequestURI = $_SERVER["REQUEST_URI"];
//remove the get query if given
if(strpos($strRequestURI, "?")) {
	$strRequestURI = strstr($strRequestURI, "?", true);
}

$strPath = "";
$strPage = "";
$strJsFile = "";

//this files will be loaded and minified
$aryJsFiles = array("jquery.js", "api.js","user.js");

//check the pages
if(strpos($strRequestURI, "/main") !== false) {
	$strPath = urldecode(substr($strRequestURI, 5));
	if($strPath == "") {
		header("Location: /main/");
	}
	$strPage = "main";
	array_push($aryJsFiles, "fs.js");
	array_push($aryJsFiles, "main.js");
} else if(strpos($strRequestURI, "/user") !== false) {
	$strPath = substr($strRequestURI, 5);
	$strPage = "user";
} else if(strpos($strRequestURI, "/login") !== false) {
	$strPath = substr($strRequestURI, 5);
	$strPage = "login";
	array_push($aryJsFiles, "login.js");
} else {
	header("Location: /main");
	exit();
}

//get the complete JavaScript code an put it in one block
$strJavaScript = "{literal}";
foreach ($aryJsFiles as $filename) {
	//get the javascript content
	$strJavaScript .= file_get_contents(JS_DIR . $filename);

	//if were not in debug mode shrink the javascript
	if(DEBUG == false) {
		$strJavaScript = str_replace("\r\n", "\n", $strJavaScript);
		$strJavaScript = str_replace("\n\r", "\n", $strJavaScript);
		$strJavaScript = str_replace("\r", "\n", $strJavaScript);

		$strJavaScript = preg_replace("/\/\/(.*?)\n/", "", $strJavaScript);
		$strJavaScript = str_replace("\n", "", $strJavaScript);
		$strJavaScript = str_replace("\t", "", $strJavaScript);
	}
}
$strJavaScript .= "{/literal}";

$objSmarty = new Smarty();
$objSmarty->assign("strPage", $strPage);
$objSmarty->assign("strJavaScript", $strJavaScript);
$objSmarty->assign("strJsFile", $strJsFile);
$objSmarty->assign("strPath", $strPath);


$objSmarty->display("templates/" . $strPage . ".tpl");
?>